<?php include('../resources/views/header.blade.php'); ?>

<style type="text/css">
    .add_question_input{
        padding: 0 ;
        width: 94% ;
        border-style: none ;
        margin-left: 3% ;
        margin-right: 3% ;
        border-radius: 0px ;
        outline: none ;
        font-size: 16px ;
        line-height: 30px;
        background-color: #F8F9FA ;
        border-bottom: 2px solid #ccc ;
    }
    .add_question_input:focus {
        padding: 0 ;
        width: 94% ;
        border-style: none ;
        margin-left: 3% ;
        margin-right: 3% ;
        border-radius: 0px ;
        outline: none ;
        font-size: 16px ;
        line-height: 30px;
        background-color: #F8F9FA ;
        border-bottom: 2px solid #58bdfd ;
    }

    .add_question_radio {
        -webkit-appearance: radio;
        height: 16px;
        vertical-align: top;
    }
</style>

<section>
    <p style="padding-left:12px;">问题</p>
    <p style="padding-left:12px;padding-right:12px;color:#666;font-size:16px;"><?php if(isset($question)){ echo $question[0]->title; } ?></p>
</section>
<section style="margin-top:40px;padding-bottom:36px;">
    <textarea class="add_question_input" id="answer_content" style="min-height:120px;" placeholder="回答" ><?php if(isset($is_edit)){
        $target = str_replace("<br />" , "" , $answer[0]->content);
        echo $target;
    } ?></textarea>
    <input type="hidden" id="question_tag" value="<?php if(isset($question)){ echo $question[0]->id; } ?>" />
    <div style="margin-top:16px;">
        <span style="float:right;color:#58bdfd;font-size:16px;margin-right:20px;" id="commit_answer">提交</span>
    </div>
</section>

<?php include('../resources/views/footer.blade.php'); ?>

<script type="text/javascript">
    $("#commit_answer").on('click',function(){
        var content         = $("#answer_content").val();
        var question_id     = $("#question_tag").val();
        var is_edit         = <?php if(isset($is_edit)){ echo $is_edit; }else{ echo 0; } ?>;
        if( content == '' ){
            toast("请填写问答的内容");return ;
        }

        content = content.replace(/(\r)*\n/g,'<br />');

        if( is_edit == 0 ){
            $.ajax({
                type: "post" ,
                url: "/answer" ,
                data: {"content":content , "question_id":question_id} ,
                dataType: "json" ,
                success: function(data){
                    if( data.error ){
                        toast(data.message);
                    }else{
                        toast("回答成功");
                        window.history.back(-1);
                    }
                }
            });
        }else{
            $.ajax({
                type: "put" ,
                url: "/answer/"+question_id ,
                data: {"content":content} ,
                dataType: "json" ,
                success: function(data){
                    if( data.error ){
                        toast(data.message);
                    }else{
                        toast("修改成功");
                        window.history.back(-1);
                    }
                }
            });
        }
    });
</script>
